<template>
  <view>
    <!-- 使用搜索自定义组件 -->
    <my-search @my-click="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <block v-for="(item, i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i === active? 'active':' ']" @click="activeChange(i)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y :scroll-top="scrollTop" :style="{height: wh + 'px'}">
        <view class="cate-level2" v-for="(item2,i2) in cateLevel2" :key="i2">
          <!-- 二级标题 -->
          <view class="cate-level2-title">/{{item2.cat_name}}/</view>
          <!-- 三级数据 -->
          <view class="cate-level3-list">
            <view class="cate-level3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoDoodsList(item3)">
              <!-- 图片 -->
              <image :src="item3.cat_icon"></image>
              <!-- 文本 -->
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    data() {
      return {
        // 当前设备的可用高度
        wh: 0,
        // 分类数据列表
        cateList: [],
        // 分类二级数据
        cateLevel2: [],
        // 激活索引
        active: 0,
        // 滚动距离
        scrollTop: 0

      };
    },
    mixins: [badgeMix],
    onLoad() {
      const systemInfo = uni.getSystemInfoSync()
      this.wh = systemInfo.windowHeight - 50
      //获取分类数据
      this.getCateList();
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        console.log(res);
        // 失败提示
        if (res.meta.status !== 200) return uni.$showMsg()
        // 成功获取
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
      },
      // 分类item点击
      activeChange(index) {
        this.active = index
        this.cateLevel2 = this.cateList[index].children
        //重置scroll
        this.scrollTop = this.scrollTop ? 0 : 1
      },
      // 跳转商城详细
      gotoDoodsList(item) {
        console.log(item);
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      // 跳转搜索页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
        console.log("2");
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        text-align: center;
        font-size: 12px;
        background-color: #F7F7F7;

        &.active {
          background-color: #FFFFFF;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);


          }
        }
      }
    }
  }

  .cate-level2-title {
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    padding: 15px 0;
  }

  .cate-level3-list {
    display: flex;
    flex-wrap: wrap;

    .cate-level3-item {
      width: 33.3%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin-bottom: 10px;

      image {
        height: 60px;
        width: 60px;

      }

      text {
        font-size: 12px;
      }
    }
  }
</style>
